var page = require('webpage').create(),
  system = require('system');
var spawn = require("child_process").spawn
 
if (system.args.length === 1) {
  console.log('Usage: netsniff.js <some URL>');
  phantom.exit(1);
} else {
  var urls = [];
  page.address = system.args[1];
  page.onResourceReceived = function (res) {
    if (res.stage === 'start') {
      urls.push(res.url);
    }
  };
  page.open(page.address, function (status) {
    var har;
    if (status !== 'success') {
      console.log('FAIL to load the address');
      phantom.exit(1);
    } else {
      console.log('down resource ' + urls.length + ' urls.');
      var child = spawn("node", ["--harmony", "downHtml.js", urls.join(',')])
      child.stdout.on("data", function (data) {
       console.log(data);
      })
      child.stderr.on("data", function (data) {
       console.log(data);
      })
      child.on("exit", function (code) {
       phantom.exit();
      })      
    }
  });
}
//http://www.baidu.com/link?url=ZXbZOZXZU-mB9ocoqKf_H4QLm0_14OUIOeDQgzeAzH8nkj9d0Ikr1AsYZqhrb6P0
//http://www.baidu.com/link?url=WLkIYik3Ttbfxic7Uv_ww5OngX8ooh2P9XkhHZetCNqql3afZFPFyZ88WzMG2sDw
//http://www.baidu.com/link?url=r69ayFx8EMIym_3TsjM-Gf6gnz2K9rEJE8gnLJmvmoi
//http://www.baidu.com/link?url=Rm_dURRTxGgoKcNTF77ZoxEifyWa6-knb07IxYILaBS
//http://www.baidu.com/link?url=_Somg8EZhJSgCbDLSW1VQeWN2WmtElOUNDzMS25lDPP11nig2bvXp_eTX589kc6G
//http://www.baidu.com/link?url=NPFxa41h7CIfjyDKhxHdrfSHNk80xJBSJcxPoI_F11JcRGFQAa5ij-A8jNAqszG1
//http://www.baidu.com/link?url=4wb5JBX4LcTmWaMi4Lc3huK0wxeCOU7kCXx7rFhtyASXfNhw59dui1WeeRRq3nFEtw4wEGSriwlNRfofalH11K
//http://www.baidu.com/link?url=AAN0EL9TudUzgV1favXIcba7pOBSCuW07kFMBRK_DkYIDDfzSolb6pzXZ8Yiw4I3
//http://www.baidu.com/link?url=JQeJLRe7K_hMoWO3UpafnXObmHzV7dgadUxqdNGNw68bA4UB6J4jlr_ndAaEzPzBNFuFJvFjy3Whrrq4ef4OXq
//http://www.baidu.com/link?url=O8MA3aOE-Usuors51Y9kMJn9VDkKh9jby3f_TWdx6drZTybzqeaXKhqZajyCMzcG
//http://www.baidu.com/link?url=KX59HQdcjLUcFmwdlTk4p5sOzTUAbygSw9gqR5ezCsm